package com.qfedu.mtl.dao.impl;

import com.qfedu.mtl.dao.SystemlogsDao;
import com.qfedu.mtl.entity.Systemlogs;
import com.qfedu.mtl.query.SystemlogsQuery;
import com.qfedu.mtl.util.DBUtil;
import com.qfedu.mtl.util.StrUtil;
import com.qfedu.mtl.util.handler.impl.BeanListHandler;
import com.qfedu.mtl.util.handler.impl.NumberHandler;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @version v1.0
 * @Author NVSonar
 * @Date 2023/9/26 21:08
 * @Email 1595220980@qq.com
 */
public class SystemlogsDaoImpl implements SystemlogsDao {
    @Override
    public void insert(Systemlogs systemlogs) {
        String sql="insert into tb_systemlogs(mgr_id,log_content,log_time) values(?,?,?)";
        DBUtil.executeDML(sql,DBUtil.getConn(),systemlogs.getMgrId(),systemlogs.getLogContent(),new Date());
    }

    @Override
    public List<Systemlogs> selectAll() {
        String sql="select * from tb_systemlogs";
        return DBUtil.executeDQL(sql,DBUtil.getConn(),new BeanListHandler<>(Systemlogs.class,true));
    }

    @Override
    public List<Systemlogs> selectData(Integer page, Integer size, SystemlogsQuery query) {
        List<Object> params = new ArrayList<>();
        StringBuilder sqlBuilder = new StringBuilder("select * from tb_systemlogs where 1=1 ");
        if(!StrUtil.isNullOrEmpty( query.getMgrId())){
            sqlBuilder.append(" and  mgr_id = ? ");
            params.add(query.getMgrId());
        }
        if( query.getLogTime()!=null ){
            sqlBuilder.append(" and log_time < ? ");
            params.add( query.getLogTime() );
        }
        sqlBuilder.append(" limit ?,?");
        params.add(  (page-1)*size  );
        params.add( size );
        return DBUtil.executeDQL(sqlBuilder.toString(), DBUtil.getConn(),new BeanListHandler<>(Systemlogs.class,true), params.toArray() );

    }

    @Override
    public Integer selectTotal(SystemlogsQuery query) {
        List<Object> params = new ArrayList<>();
        StringBuilder sqlBuilder = new StringBuilder("select count(*) from tb_systemlogs where 1=1 ");
        if(!StrUtil.isNullOrEmpty( query.getMgrId())){
            sqlBuilder.append(" and  mgr_id = ? ");
            params.add(query.getMgrId());
        }
        if( query.getLogTime()!=null ){
            sqlBuilder.append(" and log_time < ? ");
            params.add( query.getLogTime() );
        }

        return DBUtil.executeDQL(sqlBuilder.toString(), new NumberHandler<>(Long.class), params.toArray()).intValue();
    }



}
